package com.lzx.controller;

import com.alibaba.fastjson.JSON;
import com.lzx.model.FileEntity;
import com.lzx.model.User;
import com.lzx.model.nydsj.Trphz;
import com.lzx.model.qxdata.QxData;
import com.lzx.model.wlwjd.Land;
import com.lzx.model.wlwjd.Ssspdw;
import com.lzx.model.wlwjd.Wlwjd;
import com.lzx.service.FileEntityService;
import com.lzx.service.nydsj.TrphzService;
import com.lzx.service.qxdata.QxDataService;
import com.lzx.service.wlwjd.LandService;
import com.lzx.service.wlwjd.SsspdwService;
import com.lzx.service.wlwjd.WlwjdService;
import com.lzx.utils.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/sy/wlwjd")
public class WlwjdController {
    @Autowired
    private WlwjdService wlwjdService;
    @Autowired
    private FileEntityService fileBeanService;
    @Autowired
    private LandService landService;
    @Autowired
    private SsspdwService ssspdwService;
    @Autowired
    private QxDataService qxDataService;
    @Autowired
    private TrphzService trphzService;


    @RequestMapping("/wlwjd.html")
    public ModelAndView wlwjd(HttpServletRequest request){
        ModelAndView mv  = new ModelAndView("system/wlwjd");
        String landid = request.getParameter("dkid");

        String imgurl="";

        Land land = landService.findLandName(landid);

        FileEntity fileEntity = new FileEntity();
        fileEntity.setYwId(landid);
        fileEntity.setYwType("landtp");
        try {
            List<FileEntity> fileBeans = fileBeanService.selectdemolist(fileEntity);
            System.out.println(fileBeans.size());
            if(fileBeans != null && fileBeans.size()>0) {
                imgurl = fileBeans.get(0).getUrlPath();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        Ssspdw ssspdw = new Ssspdw();
        ssspdw.setLandid(land.getMassifId());
        ssspdw.setType("sp");
        List<Ssspdw> listsp = ssspdwService.findlist(ssspdw);
        ssspdw.setType("jz");
        List<Ssspdw> listjz = ssspdwService.findlist(ssspdw);
        mv.addObject("listsp",listsp);
        if(listjz != null && listjz.size()>0) {
            mv.addObject("jz", listjz.get(0));
        }else{
            mv.addObject("jz", "");
        }

        mv.addObject("land",land);
        mv.addObject("imgurl",imgurl);
        mv.addObject("detailed","哈尔滨地区地图");

        if(SessionUtil.getUser(request).getIsAdmin().equals("Y")){
            mv.addObject("isAdmn","TRUE");
        }else{
            mv.addObject("isAdmn","FALSE");
        }

        return mv;
    }

    @RequestMapping("/wlwjdnew.html")
    public ModelAndView wlwjdnew(HttpServletRequest request,HttpServletResponse response,String dkid){
        ModelAndView mv  = new ModelAndView("zhny/jianCeShuJu_xx");

        String imgurl="";

        Land land = landService.findLandName(dkid);

        FileEntity fileEntity = new FileEntity();
        fileEntity.setYwId(dkid);
        fileEntity.setYwType("landtp");
        try {
            List<FileEntity> fileBeans = fileBeanService.selectdemolist(fileEntity);
            System.out.println(fileBeans.size());
            if(fileBeans != null && fileBeans.size()>0) {
                imgurl = fileBeans.get(0).getUrlPath();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        Ssspdw ssspdw = new Ssspdw();
        ssspdw.setLandid(land.getMassifId());
        ssspdw.setType("sp");
        List<Ssspdw> listsp = ssspdwService.findlist(ssspdw);
        ssspdw.setType("jz");
        List<Ssspdw> listjz = ssspdwService.findlist(ssspdw);
        mv.addObject("listsp",listsp);
        if(listjz != null && listjz.size()>0) {
            mv.addObject("jz", listjz.get(0));
        }else{
            mv.addObject("jz", "");
        }

        mv.addObject("land",land);

        mv.addObject("imgurl",imgurl);
        //mv.addObject("detailed","哈尔滨地区地图");

        if(SessionUtil.getUser(request).getIsAdmin().equals("Y")){
            mv.addObject("isAdmn","TRUE");
        }else{
            mv.addObject("isAdmn","FALSE");
        }
        String locale = request.getParameter("locale");
//        System.out.println("index========"+locale);
        writeCookie(request,response,"locale",locale);
        User user = SessionUtil.getUser(request);
        mv.addObject("user", user);

        return mv;
    }

    @RequestMapping("/qxdatanew.html")
    public ModelAndView qxdatanew(HttpServletRequest request,HttpServletResponse response,String dkid){
        ModelAndView mv  = new ModelAndView("zhny/jianCeShuJu_xx_2");

        String imgurl="";

        Land land = landService.findLandName(dkid);

        FileEntity fileEntity = new FileEntity();
        fileEntity.setYwId(dkid);
        fileEntity.setYwType("landtp");
        try {
            List<FileEntity> fileBeans = fileBeanService.selectdemolist(fileEntity);
            System.out.println(fileBeans.size());
            if(fileBeans != null && fileBeans.size()>0) {
                imgurl = fileBeans.get(0).getUrlPath();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        Ssspdw ssspdw = new Ssspdw();
        ssspdw.setLandid(land.getMassifId());
        ssspdw.setType("sp");
        List<Ssspdw> listsp = ssspdwService.findlist(ssspdw);
        ssspdw.setType("jz");
        List<Ssspdw> listjz = ssspdwService.findlist(ssspdw);
        mv.addObject("listsp",listsp);
        if(listjz != null && listjz.size()>0) {
            mv.addObject("jz", listjz.get(0));
        }else{
            mv.addObject("jz", "");
        }

        mv.addObject("land",land);
        mv.addObject("dkid",dkid);
        mv.addObject("imgurl",imgurl);

        if(SessionUtil.getUser(request).getIsAdmin().equals("Y")){
            mv.addObject("isAdmn","TRUE");
        }else{
            mv.addObject("isAdmn","FALSE");
        }

        QxData qxData=new QxData();
       // qxData.setTablename("tbl_"+land.getUserkey());
        qxData.setTablename("tbl_18074919002");
        List<QxData> listqxdata=qxDataService.findqxsjlist(qxData);
        mv.addObject("listqxdata", JSON.toJSONString(listqxdata));

        QxData qxData2=new QxData();
        qxData2.setTablename("tbl_18074919003");
        List<QxData> listqxdata2=qxDataService.findqxsjlist(qxData2);
        mv.addObject("listqxdata2",JSON.toJSONString(listqxdata2));

        Trphz trphz=new Trphz();
        trphz.setMassifId(dkid);
        Trphz ph=trphzService.querybymassifId(trphz);
        mv.addObject("ph",ph.getPh());


        String locale = request.getParameter("locale");
        writeCookie(request,response,"locale",locale);


        return mv;
    }
    public static void writeCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String value) {
        Cookie cookie = new Cookie(cookieName, value);
        cookie.setPath("/");
        cookie.setHttpOnly(true);
        response.addCookie(cookie);
    }


}
